﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

/// <summary>
///TemplateRunSqlSearchRun 的摘要说明
/// </summary>
public class TemplateRunSqlSearchRun:ISearchRunType
{
	public TemplateRunSqlSearchRun()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    #region ISearchRunType 成员

    public string GetSearchString(ConfigColumns cCol, string Value)
    {
        if (string.IsNullOrEmpty(cCol.SearchShowRunSql))
        {
            Page page=HttpContext.Current.Handler as Page;
            Control tempCtrl = page.LoadControl(cCol.SearchShowTemplatePath);
            if (tempCtrl is ISearchRunType)
            {
                return ((ISearchRunType)tempCtrl).GetSearchString(cCol, Value);
            }
            else
            {
                throw new Exception(cCol.ColumnName + "在搜索时发生错误：既没有定义搜索SQL语句，模板("+cCol.SearchShowTemplatePath+")也没有实现ISearchRunType接口");
            }
        }
        string[] param = cCol.SearchShowTemplateValuesFrom.Split(',');
        string r = cCol.SearchShowRunSql;
        for (int i = 0; i < param.Length; i++)
        {
            HttpRequest Request = HttpContext.Current.Request;
            string reqV = Request.Form[cCol.ColumnName + "_SearchShow$" + param[i]];
            r = r.Replace("{" + i + "}", reqV.Replace("'","''"));
        }

        return r;
    }

    #endregion
}
